package com.tusuo.stds.ui.question;

import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.tusuo.stds.R;
import com.tusuo.stds.rest.LevelDetail;
import com.tusuo.stds.rest.LevelRecordDetail;
import com.tusuo.stds.rest.LevelRecordList;
import com.tusuo.stds.rest.Question;
import com.tusuo.stds.ui.BaseToolbarActivity;
import com.tusuo.stds.ui.ToolbarOwner;
import com.tusuo.stds.util.webview.WebViewManager;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.StringRes;
import org.parceler.Parcels;

import java.util.ArrayList;
import java.util.List;

import myandroid.library.util.ApplicationHelper;

import static com.tusuo.stds.ui.question.QuestionActivity.QUESTION_TYPE_MULTI;
import static com.tusuo.stds.ui.question.QuestionActivity.QUESTION_TYPE_SINGLE;
import static com.tusuo.stds.ui.question.QuestionActivity.QUESTION_TYPE_TF;
import static com.tusuo.stds.ui.question.QuestionActivity.QUESTION_TYPE_UNSURE;

@EActivity(R.layout.activity_level_record_detail_temp)
public class LevelRecordDetailActivity extends BaseToolbarActivity<LevelRecordDetailView,LevelRecordDetailPresenter> implements LevelRecordDetailView {

    @ViewById(R.id.level_record_detail_tv_no_question)
    TextView tvNoQuestions;

    @ViewById(R.id.level_record_detail_tv_type)
    TextView tvType;
    @ViewById(R.id.level_record_detail_tv_count)
    TextView tvCount;

    @ViewById(R.id.level_record_detail_vp)
    ViewPager viewPager;

    @StringRes(R.string.question_type_single)
    String strTypeSingle;
    @StringRes(R.string.question_type_multi)
    String strTypeMulti;
    @StringRes(R.string.question_type_tf)
    String strTypeTF;
    @StringRes(R.string.question_type_unsure)
    String strTypeUnsure;


    LevelRecordList.LevelRecord mLevelRecord;

    LevelRecordDetail mLevelRecordDetail;

    String mSecretKey;
    private List<WebViewManager> mWebViewManagerList;
    private List<View> listView;
    private boolean isLevel = true;
    private LevelRecordDetail mCompleteDetail;

    @NonNull
    @Override
    public LevelRecordDetailPresenter createPresenter() {
        return new LevelRecordDetailPresenter(this, mLevelRecord, isLevel);
    }

    @AfterInject
    void init() {
        mCompleteDetail = Parcels.unwrap(getIntent().getParcelableExtra(LevelRecordDetail.NAME));
        mLevelRecord = Parcels.unwrap(getIntent().getParcelableExtra(LevelRecordList.LevelRecord.EXTRA_NAME));
        isLevel = getIntent().getBooleanExtra("isLevel",true);
        mWebViewManagerList = new ArrayList<>();
        listView = new ArrayList<>();
    }

    @AfterViews
    void initData() {
        setupUI(this);

        String toolbarTitle;
        if (isLevel) {
            toolbarTitle = mLevelRecord.Title;
        }else {
            toolbarTitle = mCompleteDetail.Title;
        }

        List<ToolbarOwner.MenuAction> menuActionList = new ArrayList<>();

        ToolbarOwner.Config config = new ToolbarOwner.Config(toolbarTitle, true, menuActionList);
        mToolbarOwner.setConfig(config);

        ApplicationHelper.getInstance().addActivity(this);

        if (!isLevel) {
            initAdapter(mCompleteDetail, isLevel);
        }
    }

    public void initAdapter(LevelRecordDetail levelRecordDetail,boolean isLevel) {
        mLevelRecordDetail = levelRecordDetail;

        LevelDetail detail = new LevelDetail();
        detail.QuestionCount = mLevelRecordDetail.QuestionCount;
        detail.QuestionList = mLevelRecordDetail.QuestionList;
        detail.Description = mLevelRecordDetail.Description;

        for (int i = 0;i < mLevelRecordDetail.QuestionCount;i ++) {
            View view = View.inflate(this, R.layout.item_question, null);
            BridgeWebView webView = (BridgeWebView) view.findViewById(R.id.item_question_webview);
            WebViewManager webViewManager = new WebViewManager(this, webView);

            Question question = mLevelRecordDetail.QuestionList.get(i);
            question.Text = question.getDecodedText(mSecretKey);
            question.QuestionAnalysis = question.getDecodedQuestionAnalysis(mSecretKey);
            for (Question.QuestionOption option : question.QuestionOptionList) {
                option.Content = option.getDecodedContent(mSecretKey);
            }

            listView.add(view);
            mWebViewManagerList.add(webViewManager);
        }

        QuestionAdapterForLevelRecord adapter = new QuestionAdapterForLevelRecord(listView,detail, mWebViewManagerList,mSecretKey,this);

        viewPager.setOffscreenPageLimit(2);
        viewPager.setAdapter(adapter);

        initText(mLevelRecordDetail.QuestionList.get(0),0);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if (state == ViewPager.SCROLL_STATE_IDLE) {
                    initText(mLevelRecordDetail.QuestionList.get(viewPager.getCurrentItem()),viewPager.getCurrentItem());
                }
            }
        });
    }

    @Override
    public void initAdapter(LevelRecordDetail levelRecordDetail) {
        mLevelRecordDetail = levelRecordDetail;

        LevelDetail detail = new LevelDetail();
        detail.QuestionCount = mLevelRecordDetail.QuestionCount;
        detail.QuestionList = mLevelRecordDetail.QuestionList;
        detail.Description = mLevelRecordDetail.Description;

        for (int i = 0;i < mLevelRecordDetail.QuestionCount;i ++) {
            View view = View.inflate(this, R.layout.item_question, null);
            BridgeWebView webView = (BridgeWebView) view.findViewById(R.id.item_question_webview);
            WebViewManager webViewManager = new WebViewManager(this, webView);

            Question question = mLevelRecordDetail.QuestionList.get(i);
            question.Text = question.getDecodedText(mSecretKey);
            question.QuestionAnalysis = question.getDecodedQuestionAnalysis(mSecretKey);
            for (Question.QuestionOption option : question.QuestionOptionList) {
                option.Content = option.getDecodedContent(mSecretKey);
            }

            listView.add(view);
            mWebViewManagerList.add(webViewManager);
        }

        QuestionAdapterForLevelRecord adapter = new QuestionAdapterForLevelRecord(listView,detail, mWebViewManagerList,mSecretKey,this);

        viewPager.setOffscreenPageLimit(2);
        viewPager.setAdapter(adapter);

        initText(mLevelRecordDetail.QuestionList.get(0),0);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if (state == ViewPager.SCROLL_STATE_IDLE) {
                    initText(mLevelRecordDetail.QuestionList.get(viewPager.getCurrentItem()),viewPager.getCurrentItem());
                }
            }
        });
    }

    public void initText(Question question, int index) {
        switch (question.QuestionType) {
            case QUESTION_TYPE_SINGLE:
                tvType.setText(strTypeSingle);
                break;
            case QUESTION_TYPE_MULTI:
                tvType.setText(strTypeMulti);
                break;
            case QUESTION_TYPE_TF:
                tvType.setText(strTypeTF);
                break;
            case QUESTION_TYPE_UNSURE:
                tvType.setText(strTypeUnsure);
                break;
            default:
                tvType.setText("未知");
                break;
        }
        tvCount.setText(getString(R.string.question_count, index + 1, mLevelRecordDetail.QuestionCount));
    }

    @Override
    public void setSecretKey(String secretKey) {
        mSecretKey = secretKey;
    }

    @Override
    public void initFirst() {

    }
}
